import axios from 'axios';
const request = axios.create({
    // easy-mock服务挂了，暂时不使用了
    timeout: 20000,
    baseURL: '/api'
});

request.interceptors.request.use(
    config => {
        const token = localStorage.getItem('access_token');
        if (token) config.headers['Authorization'] = token ;
        return config;
    },
    error => {
        return Promise.reject();
    }
);

request.interceptors.response.use(
    //当status是2xx时
    response => {
        if (response.data.code === 200) {
            return Promise.resolve(response.data);
        }else {
            return Promise.reject(response.data);
        }
    },
    error => {
        // if (error.response) {
        //     switch (error.response.status) {
        //         case 401:
        //             vue.$router.replace('/login');
        //             break;
        //         case 403:
        //             vue.$router.replace('/login');
        //             break;
        //     }
        // }
        return Promise.reject(error);
    }
);

export default request;
